//
//  HRDiscoverSearchBar.swift
//  weiBo
//
//  Created by helong on 16/7/12.
//  Copyright © 2016年 helong. All rights reserved.
//

import UIKit

class HRDiscoverSearchBar: UIView,UITextFieldDelegate {


    @IBOutlet weak var searchTextRight: NSLayoutConstraint!
    @IBOutlet weak var searchText: UITextField!
    @IBOutlet weak var cancelButton: UIButton!
    
     // MARK: - 取消按钮的点击事件
    @IBAction func cancelButton(sender: AnyObject) {
        //点击取消,文本框变大,覆盖按钮
//       将文本框的约束属性拖线, 改变文本框的大小,显示按钮
        searchTextRight.constant = 0
            UIView.animateWithDuration(0.5, animations: { () -> Void in
                self.layoutIfNeeded()
                //并辞去第一响应者
                self.searchText.resignFirstResponder()
            })
        
            }
    
    // MARK: - 文本框开始编辑
    func textFieldDidBeginEditing(textField: UITextField) {
        
//       将文本框的约束属性拖线, 改变文本框的大小,显示按钮
        searchTextRight.constant = cancelButton.frame.size.width

            UIView.animateWithDuration(0.4, animations: { () -> Void in
                self.layoutIfNeeded()
            })
        

    }
    
    class func loadDiscoverSearchBar() -> HRDiscoverSearchBar{
//       return NSBundle .mainBundle().loadNibNamed("HRDiscoverSearchBar", owner: nil, options: nil).last as! HRDiscoverSearchBar
        return UINib(nibName: "HRDiscoverSearchBar", bundle: nil).instantiateWithOwner(nil, options: nil).last as! HRDiscoverSearchBar
        
    }
    
}
